package com.gl.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gl.order.commom.ResultBean;
import com.gl.order.entity.TUser;
import com.gl.order.entity.dto.AddCustomerDto;
import com.gl.order.entity.dto.PageDto;
import com.gl.order.entity.vo.CustomerPageVo;
import com.gl.order.security.model.SecurityUser;
import com.gl.order.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author: liangSY
 * @Date: 2021/6/26
 * @ClassName: CustomerController
 * @Description: CustomerController描述
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerController {

    CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @PostMapping("/pages")
    @ApiOperation("获取订单列表")
    public ResultBean<IPage<CustomerPageVo>> list(@RequestBody @Valid PageDto dto){
        SecurityUser SecurityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        TUser user = SecurityUser.getCurrentUserInfo();
        String marketUser = user.getId();
        for (String role : SecurityUser.getPermissionsList()) {
            marketUser = !"admin".equals(role) ? marketUser : "";
        }
        return ResultBean.success(customerService.getList(dto, marketUser));
    }

    @ApiOperation("添加客户")
    @PostMapping("/add")
    public ResultBean add(@RequestBody @Valid AddCustomerDto dto) {
        customerService.add(dto);
        return ResultBean.success();
    }

    @ApiOperation("获取客户信息")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "客户id", dataType = "String", paramType = "path")
    public ResultBean getCustomer(@PathVariable("id") String id) {
        return ResultBean.success(customerService.findById(id));
    }

    @ApiOperation("获取客户信息")
    @GetMapping("/remove")
    @ApiImplicitParam(name = "id", value = "客户id")
    public ResultBean delCustomer(@RequestParam("id") String id) {
        return ResultBean.success(customerService.delCustomer(id));
    }

}
